<template>
	<view>
		<view class="tip ">
			· 智慧社区 ·
		</view>
		<view class="login-box centerXY-g" @click="login">授权登录</view>
	</view>
</template>

<script>
	import {
		wxlogin
	} from "@/common/http.api.js"
	export default {
		data() {
			return {
				userdata: {}
			}
		},
		methods: {
			login() {
				let _this = this
				uni.$loading('正在登录...')
				wx.login({
					async success(resu) {
						console.log(resu);
						if (resu.code) {
							// 获取用户code
							
							const code = resu.code
							// 判断是否注册
							const result = await wxlogin({
								code
							})
							uni.setStorageSync('uID', result.data.uID)
							uni.setStorageSync('token', result.token)
							_this.$store.commit('getUserInfo', result.data)
							if (result.data.isRegister === 0) {
								console.log('?');
								// 未注册跳转注册
								uni.navigateTo({
									url: '/pages/login/login'
								})
							} else {
								// 已注册直接到主页
								uni.switchTab({
									url: '/pages/home/home'
								})
							}
						}
					}
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.tip {
		position: fixed;
		font-weight: bold;
		top: 30%;
		left: 50%;
		transform: translateX(-50%);
	}

	.login-box {
		position: fixed;
		padding: 0 40rpx;
		background-color: #4d7ff2;
		box-shadow: 0 2rpx 20rpx 0 rgba(0, 0, 0, 0.08);
		width: 80%;
		height: 90rpx;
		border-radius: 20rpx;
		color: #fff;
		font-weight: bold;
		font-size: 16px;
		text-align: center;
		line-height: 90rpx;
	}

	.back-box {
		transform: translate(-50%, 100%);
		background-color: #fff;
		color: gray;
	}

	.login-box:hover {
		background-color: #96c1ff;
	}

	.back-box:hover {
		background-color: #eee;
	}
</style>
